package com.zd.mock.liyuxian;

import com.github.javafaker.Faker;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class ReviewDataGenerator {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/fast_food?useSSL=false&requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final Faker faker = new Faker();
    private static final Random random = new Random();
    private static final String[] attractions = {"少林寺", "黄河风景区", "郑州动物园", "河南博物院", "郑州方特欢乐世界", "郑州海洋馆"};
    private static final String[] comments = {
            "少林寺非常壮观，值得一去",
            "酒店服务很好，房间干净整洁",
            "两日游安排合理，导游专业",
            "工艺品很有地方特色",
            "海洋馆适合带孩子游玩",
            "酒店设施一流，景观很棒"
    };
    private static final String[] phoneNumbers = {
            "13800138001",
            "13800138002",
            "13800138003",
            "13800138004",
            "13800138005",
            "13800138006"
    };

    public static void main(String[] args) {
        try {
            generateReviews(10);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void generateReviews(int count) throws SQLException {
        String insertSql = "INSERT INTO visitor_reviews (review_type, business_id, visitor_phone, rating, comment, visit_date, create_time) VALUES (?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(insertSql)) {

            conn.setAutoCommit(false);

            for (int i = 1; i <= count; i++) {
                int reviewType = random.nextInt(4) + 1;
                int businessId = random.nextInt(6) + 1;
                String visitorPhone = phoneNumbers[random.nextInt(phoneNumbers.length)];
                int rating = random.nextInt(5) + 1;
                String comment = comments[random.nextInt(comments.length)];
                LocalDateTime visitDate = LocalDateTime.now().minusDays(random.nextInt(30));
                LocalDateTime createTime = LocalDateTime.now().minusDays(random.nextInt(30));

                pstmt.setInt(1, reviewType);
                pstmt.setInt(2, businessId);
                pstmt.setString(3, visitorPhone);
                pstmt.setInt(4, rating);
                pstmt.setString(5, comment);
                pstmt.setTimestamp(6, Timestamp.valueOf(visitDate));
                pstmt.setTimestamp(7, Timestamp.valueOf(createTime));

                pstmt.addBatch();
                if (i % 100 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                }
            }

            pstmt.executeBatch();
            conn.commit();
            System.out.println("成功插入 " + count + " 条 visitor_reviews 数据");

        } catch (SQLException e) {
            throw e;
        }
    }
}